package top.yesjava.webfinal.web;

import top.yesjava.webfinal.common.framework.simplemvc.RequestMapping;
import top.yesjava.webfinal.common.utils.JSONUtil;
import top.yesjava.webfinal.common.utils.PageInfo;
import top.yesjava.webfinal.common.utils.Result;
import top.yesjava.webfinal.entity.Student;
import top.yesjava.webfinal.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class AjaxDemoController {
    StudentService studentService = new StudentService();

    /*
    浏览器 内部有一个javaScript 对象叫 xmlhttprequest,它相当于浏览器的地址栏，也可以发请求，接响应
     */

    @RequestMapping("/ajax_demo.do")
    public String ajaxDemo(HttpServletRequest request, HttpServletResponse response){
        return  "ajaxDemo";
    }


    /*
      控制层的业务方法没有后返回值，通过 response  输出json 对象
     */
    @RequestMapping("/msg.do")
    public void msgDemo(HttpServletRequest request, HttpServletResponse response){
//        以json 的方式给浏览器返回数据，就是 js 的对象格式键值对  {"name":"张三","age":18,"sex":"女"}
 /*       Map<String,Object> map = new HashMap<>();
        map.put("msg","原来ajax 就是这么回事");*/
       /* map.put("name","张三丰");
        map.put("age",90);
        map.put("sex","男");*/

//
        Student student = studentService.getStudentById(22);

        JSONUtil.printByJSON(response, Result.success(student));
    }








}
